<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/4/13
 * Time: 9:31
 */

namespace app\index\controller;
use app\index\model\CrmMedia;

class Media extends Base
{
    /*
     * 添加和修改,软删除
     * */
    public function update(){
        $data = input();
        $CrmMediaModel = new CrmMedia;
        if(!isset($data['media'])) apiReturn(400,'请输入媒体渠道');
        if(!isset($data['type'])) apiReturn(400,'请选择类型');
        if(!isset($data['price'])) apiReturn(400,'价格不能为空');
        if ($data['ticket'] == 1) {
            if(!isset($data['ticket_name'])) apiReturn(400,'优惠券名称不能为空');
            if(!isset($data['ticket_starttime']) && !isset($data['ticket_endtime'])) apiReturn(400,'优惠券时间不能为空');
            if(!isset($data['ticket_price'])) apiReturn(400,'优惠券价格不能为空');
            $data['ticket_starttime'] = strtotime($data['ticket_starttime']);
            $data['ticket_endtime'] = strtotime($data['ticket_endtime']);
        }else{
            unset($data['ticket_name']);
            unset($data['ticket_starttime']);
            unset($data['ticket_endtime']);
            unset($data['ticket_price']);
        }
        $data['update_time']=time();
        if (empty($data['youxiao_status'])) {
            $data['youxiao_status'] = null;
        }else{
            if (is_array($data['youxiao_status'])) {
                $data['youxiao_status'] = implode(',', $data['youxiao']);
            }else{
                $data['youxiao_status'] = $data['youxiao_status'];
            }
        }
        if(isset($data['id'])){
            unset($data["create_time"]);
            if($CrmMediaModel->allowField(true)->save($data,['id'=>$data['id']])){
                apiReturn(200,'修改成功');
            }
        }
        $media = $CrmMediaModel->where(['media'=>$data['media']])->value('media');
        if($media)apiReturn(200,'该媒体已经添加：'.$media);
        $data['create_time']=time();
        if($CrmMediaModel->allowField(true)->data($data)->save()){
            apiReturn(200,'添加成功');
        }
        apiReturn(400,'失败');
    }

    /*
     * 查询
     * */
    public function query(){
        $query = input();
        $limit = isset($query['limit'])?$query['limit']:10;
        $page = isset($query['page'])?$query['page']:1;
        $CrmMediaModel = new CrmMedia();
        $where = [];
        if (isset($query['type'])){
            $where['type'] =$query['type'];
        }
        if (isset($query['state'])){
            $where['state'] =$query['state'];
        }
        if (isset($query['media'])){
            $where['media'] = ['like',"%".$query['media']."%"];
        }
        $data['total']=  $CrmMediaModel->where($where)->count();
        $data['list'] = $CrmMediaModel->where($where)->limit($limit)->page($page)->select();
        apiReturn(200,'成功',$data);
    }

    // 添加修改子来源渠道
    public function mediaNextAdd()
    {
        $data = input();//token id media_id type 
        if(!isset($data['media_id'])) apiReturn(201,'渠道id不能为空');
        if(!isset($data['type'])) apiReturn(201,'渠道type值不能为空');
        if(db('crm_media_next')->where('type',$data['type'])->find()) apiReturn(201,'此链已添加');
        if(isset($data['id']) && !empty($data['id'])){
            db('crm_media_next')->where('id',$data['id'])->update(['type'=>$data['type']]);
        }else{
            $data['date'] = time();
            unset($data['id']);
            db('crm_media_next')->insert($data);
        }
        apiReturn(200,'操作成功');
    }

    // 子来源链接列表
    public function mediaNextList()
    {
        $data = input();//token media_id rows page
        if(!isset($data['media_id'])) apiReturn(201,'渠道id不能为空');
        $datas['list'] = db('crm_media_next')->where('media_id',$data['media_id'])->limit(($data['page']-1)*$data['rows'],$data['rows'])->order('id DESC')->select();
        $datas['count'] = db('crm_media_next')->where('media_id',$data['media_id'])->count();
        apiReturn(200,'获取子来源链接列表成功',$datas);
    }

    // 删除子来源链接
    public function mediaNextDelete()
    {
        $data = input();//token id
        if (empty($data['id'])) {
            apiReturn(201,'子来源链接id不能为空');
        }
        db('crm_media_next')->where('id',$data['id'])->delete();
        apiReturn(200,'删除成功');
    }



}